var t = require("../../../utils/qiniuUploader"), a = getApp();

Page({
    data: {
        content: "",
        src: "",
        duration: 0,
        size: 0,
        height: 0,
        width: 0,
        uploading: !1,
        progress: 0,
        qiniuVideoToken: "",
        uploadKey: "",
        youkuCover: "",
        youkuUrl: "",
        isAddWork: 1
    },
    inputChange: function(t, a) {
        this.setData({
            content: t.detail.content
        });
    },
    updateQiNiuUploadVideoToken: function() {
        var t = this;
        a.network.post({
            url: a.url.getQiNiuUploadVideoToken,
            params: {},
            before: function() {},
            success: function(a) {
                1 == a.data.result && t.setData({
                    qiniuVideoToken: a.data.data.token,
                    uploadKey: a.data.data.uploadKey
                });
            },
            fail: function() {},
            complete: function() {}
        });
    },
    addWordChange: function(t) {
        var a = this.data.isAddWork;
        a = a ? 0 : 1, this.setData({
            isAddWork: a
        });
    },
    clickYoukuDel: function(t) {
        this.setData({
            youkuCover: "",
            youkuUrl: ""
        });
    },
    clickYouku: function(t) {
        wx.navigateTo({
            url: "/pages/add/addVideoSearch/addVideoSearch?type=0"
        });
    },
    clickTencent: function(t) {
        wx.navigateTo({
            url: "/pages/add/addVideoSearch/addVideoSearch?type=1"
        });
    },
    clickLocal: function(t) {
        this.bindSelectVideoTap();
    },
    bindSelectVideoTap: function() {
        var t = this;
        wx.chooseVideo({
            sourceType: [ "album", "camera" ],
            maxDuration: 60,
            camera: "back",
            success: function(o) {
                t.setData({
                    src: o.tempFilePath,
                    duration: o.duration,
                    size: o.size,
                    height: o.height,
                    width: o.width
                }), t.uploadVideoToQiniu(o.tempFilePath, a.util.getQiniuFileName(0));
            }
        });
    },
    uploadVideoToQiniu: function(a, o) {
        var e = this;
        e.setData({
            uploading: !0
        }), t.upload(a, function(t) {
            e.setData({
                uploading: !1
            });
        }, function(t) {}, {
            region: "ECN",
            uptoken: this.data.qiniuVideoToken,
            uploadURL: "https://up.qbox.me",
            key: this.data.uploadKey,
            domain: "i.hunyu.co/"
        }, function(t) {});
    },
    bindSubmitTap: function() {
        var t, o = this, e = this.data.youkuUrl;
        if (e.length > 0) t = e; else {
            if (this.data.src.length < 3 || 0 == this.data.uploadKey.length) return void a.util.showToast("请先选择视频");
            t = o.data.uploadKey;
        }
        1 != this.data.uploading ? a.network.post({
            url: a.url.addVideo,
            params: {
                content: o.data.content,
                url: t,
                publish: 1 == o.data.isAddWork ? "1" : "0"
            },
            before: function() {
                wx.showLoading({
                    title: "提交数据"
                });
            },
            success: function(t) {
                if (wx.hideLoading(), 1 == t.data.result) {
                    wx.showToast({
                        title: "发布成功"
                    });
                    var o = getCurrentPages();
                    o[o.length - 2].setData({
                        needClose: !0
                    }), setTimeout(function() {
                        wx.navigateBack({}), wx.showTabBar({});
                    }, 1500);
                } else a.util.showToast(t.data.message);
            },
            fail: function() {
                wx.hideLoading(), a.util.showToast("提交数据失败");
            },
            complete: function() {
                o.setData({
                    uploading: !1,
                    progress: 0
                });
            }
        }) : a.util.showToast("视频正在上传，请稍等");
    },
    onLoad: function(t) {
        this.updateQiNiuUploadVideoToken();
        var o = wx.getStorageSync(a.storage.meInfo).role, e = 3 != o && 5 != o;
        this.setData({
            isAddWork: e
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = a.util.getShareInfo();
        return {
            title: a.globalData.commonShareTitle,
            path: t[0],
            imageUrl: t[1],
            success: function(t) {},
            fail: function(t) {}
        };
    }
});